package com.alibaba.dingtalk.openapi.message.chat;

import com.alibaba.dingtalk.openapi.message.Message;
import com.alibaba.dingtalk.openapi.utils.HttpHelper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.choudou5.solr.util.JsonUtil;
import com.dingtalk.open.client.api.model.corp.ChatInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Name：聊天组 助手
 * @Author：choudou5
 * @Date：2018-01-29
 * @Site：http://www.hsolr.com
 * @License：MIT
 * @Copyright：hsolr@hotmail.com (@Copyright 2018-2020)
 */
public class ChatGroupHelper {

    //doc: https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.bpMc6I&treeId=385&articleId=104977&docType=1#s1

    /**
     * 创建群组
     * @param accessToken
     * @param chatInfo
     * @return
     * @throws Exception
     */
    public static String createChat(String accessToken, ChatInfo chatInfo)
            throws Exception {
        String url = "/chat/create?access_token="+accessToken;
        JSONObject jsonObject = HttpHelper.httpPost(url, chatInfo);
        System.out.println(JSON.toJSONString(jsonObject));
        return jsonObject.getString("chatid");
    }

    /**
     * 获得 群组信息
     * @param accessToken
     * @param chatid 群组ID
     * @return
     * @throws Exception
     */
    public static ChatInfo getChatInfo(String accessToken, String chatid)
            throws Exception {
        String url = "/chat/get?access_token="+accessToken+"&chatid="+chatid;
        JSONObject jsonObject = HttpHelper.httpGet(url);
        return jsonObject.toJavaObject(ChatInfo.class);
    }

    /**
     *  发送 群组信息
     * @param accessToken
     * @param chatid
     * @param message
     * @throws Exception
     */
    public static void sendMsg(String accessToken, String chatid, Message message)
            throws Exception {
        String url = "/chat/send?access_token="+accessToken;
        Map params = new HashMap<>();
        params.put("chatid", chatid);
        params.put("msgtype", message.type());
//        params.put("isAtAll", true);

//        ArrayList<String> atMobiles = new ArrayList<String>();
//        atMobiles.add("13632676703");
//        params.put("msgsInfo", "[atList:]");

        params.put(message.type(), JsonUtil.toStr(message));
        HttpHelper.httpPost(url, params);
    }

}
